options compress=YES;

libname original ''; 
libname fra '';

%let dir =  ;
libname dir '' ; 

* MERGE MBR AND PHUS ;

	%macro array2d(var);
	array &var.(1984:2016, 1:12) 
		&var._1984_1-&var._1984_12
		&var._1985_1-&var._1985_12
		&var._1986_1-&var._1986_12
		&var._1987_1-&var._1987_12
		&var._1988_1-&var._1988_12
		&var._1989_1-&var._1989_12
		&var._1990_1-&var._1990_12
		&var._1991_1-&var._1991_12
		&var._1992_1-&var._1992_12
		&var._1993_1-&var._1993_12
		&var._1994_1-&var._1994_12
		&var._1995_1-&var._1995_12
		&var._1996_1-&var._1996_12
		&var._1997_1-&var._1997_12
		&var._1998_1-&var._1998_12
		&var._1999_1-&var._1999_12
		&var._2000_1-&var._2000_12
		&var._2001_1-&var._2001_12
		&var._2002_1-&var._2002_12
		&var._2003_1-&var._2003_12
		&var._2004_1-&var._2004_12
		&var._2005_1-&var._2005_12
		&var._2006_1-&var._2006_12
		&var._2007_1-&var._2007_12
		&var._2008_1-&var._2008_12
		&var._2009_1-&var._2009_12
		&var._2010_1-&var._2010_12
		&var._2011_1-&var._2011_12
		&var._2012_1-&var._2012_12
		&var._2013_1-&var._2013_12
		&var._2014_1-&var._2014_12
		&var._2015_1-&var._2015_12
		&var._2016_1-&var._2016_12
  	%mend array2d;

	%macro drop(var);
	drop 
			&var._1984_1-&var._1984_12
			&var._1985_1-&var._1985_12
			&var._1986_1-&var._1986_12
			&var._1987_1-&var._1987_12
			&var._1988_1-&var._1988_12
			&var._1989_1-&var._1989_12
			&var._1990_1-&var._1990_12
			&var._1991_1-&var._1991_12
			&var._1992_1-&var._1992_12
			&var._1993_1-&var._1993_12
			&var._1994_1-&var._1994_12
			&var._1995_1-&var._1995_12
			&var._1996_1-&var._1996_12
			&var._1997_1-&var._1997_12
			&var._1998_1-&var._1998_12
			&var._1999_1-&var._1999_12
			&var._2000_1-&var._2000_12
			&var._2001_1-&var._2001_12
			&var._2002_1-&var._2002_12
			&var._2003_1-&var._2003_12
			&var._2004_1-&var._2004_12
			&var._2005_1-&var._2005_12
			&var._2006_1-&var._2006_12
			&var._2007_1-&var._2007_12
			&var._2008_1-&var._2008_12
			&var._2009_1-&var._2009_12
			&var._2010_1-&var._2010_12
			&var._2011_1-&var._2011_12
			&var._2012_1-&var._2012_12
			&var._2013_1-&var._2013_12
			&var._2014_1-&var._2014_12
			&var._2015_1-&var._2015_12
			&var._2016_1-&var._2016_12;
	%mend drop;

* Unzip files ;
%macro unzip(dsn);
data _null_;
       infile inzip
        lrecl=256 recfm=F length=length eof=eof unbuf;
       file   ds lrecl=256 recfm=N;
       input;
       put _infile_ $varying256. length;
       return;
     eof:
       stop;
    run;
%mend unzip;


%macro mbrphus(seg);

data fra.mbrphus&seg._primary;
	merge 

	work.mbr_seg&seg.
	(keep=can bic dobp_: dodp_: sexprimary race doei_: doec_: dob_: bdod_: 
	tod1 desc1 pied_: pia: efd_mm: efd_ccyy: mba: mbp:)

	work.phus_seg&seg.
	(keep=can phus:
	rename=(phus8401-phus8412=phusprim_1984_1-phusprim_1984_12
			phus8501-phus8512=phusprim_1985_1-phusprim_1985_12
			phus8601-phus8612=phusprim_1986_1-phusprim_1986_12
			phus8701-phus8712=phusprim_1987_1-phusprim_1987_12
			phus8801-phus8812=phusprim_1988_1-phusprim_1988_12
			phus8901-phus8912=phusprim_1989_1-phusprim_1989_12
			phus9001-phus9012=phusprim_1990_1-phusprim_1990_12
			phus9101-phus9112=phusprim_1991_1-phusprim_1991_12
			phus9201-phus9212=phusprim_1992_1-phusprim_1992_12
			phus9301-phus9312=phusprim_1993_1-phusprim_1993_12
			phus9401-phus9412=phusprim_1994_1-phusprim_1994_12
			phus9501-phus9512=phusprim_1995_1-phusprim_1995_12
			phus9601-phus9612=phusprim_1996_1-phusprim_1996_12
			phus9701-phus9712=phusprim_1997_1-phusprim_1997_12
			phus9801-phus9812=phusprim_1998_1-phusprim_1998_12
			phus9901-phus9912=phusprim_1999_1-phusprim_1999_12
			phus0001-phus0012=phusprim_2000_1-phusprim_2000_12
			phus0101-phus0112=phusprim_2001_1-phusprim_2001_12
			phus0201-phus0212=phusprim_2002_1-phusprim_2002_12
			phus0301-phus0312=phusprim_2003_1-phusprim_2003_12
			phus0401-phus0412=phusprim_2004_1-phusprim_2004_12
			phus0501-phus0512=phusprim_2005_1-phusprim_2005_12
			phus0601-phus0612=phusprim_2006_1-phusprim_2006_12
			phus0701-phus0712=phusprim_2007_1-phusprim_2007_12
			phus0801-phus0812=phusprim_2008_1-phusprim_2008_12
			phus0901-phus0912=phusprim_2009_1-phusprim_2009_12
			phus1001-phus1012=phusprim_2010_1-phusprim_2010_12
			phus1101-phus1112=phusprim_2011_1-phusprim_2011_12
			phus1201-phus1212=phusprim_2012_1-phusprim_2012_12
			phus1301-phus1312=phusprim_2013_1-phusprim_2013_12
			phus1401-phus1412=phusprim_2014_1-phusprim_2014_12
			phus1501-phus1512=phusprim_2015_1-phusprim_2015_12
			phus1601-phus1612=phusprim_2016_1-phusprim_2016_12
			)
	)
	
	;

	by can;
run;

data fra.mbrphus&seg._primary;
	set fra.mbrphus&seg._primary;

	dobp_ccyy=input(dobp_ccyy,4.);
run;


data fra.mbrphus&seg._primary;
	set fra.mbrphus&seg._primary;

	* FIND DATE OF FIRST PAYMENT (PHUS) ;
	%array2d(phusprim);
	%array2d(n_phusany);
	%array2d(n_temp);
	%array2d(n_first);

	array phusanyyr(1984:2016) n_phusanyyr_1984-n_phusanyyr_2016;
	
	n_first_s=0;
	do i = 1984 to 2016;
		do j = 1 to 12;
			n_temp(i,j) = SUM(OF n_first_:);
			if phusprim(i,j)>0 then n_phusany(i,j)=1;
				else n_phusany(i,j)=0;
			if phusprim(i,j)>0 and n_temp(i,j)=0 then n_first(i,j)=1;
				else n_first(i,j)=0;
		end;
	end;
	
	%macro a(yr);
		n_phusanyyr_&yr.=max(OF n_phusany_&yr._:);
	%mend a;

	%a(1984); %a(1985); %a(1986); %a(1987); %a(1988); %a(1989); 
	%a(1990); %a(1991); %a(1992); %a(1993); %a(1994); %a(1995); %a(1996); %a(1997); %a(1998); %a(1999); 
	%a(2000); %a(2001); %a(2002); %a(2003); %a(2004); %a(2005); %a(2006); %a(2007); %a(2008); %a(2009); 
	%a(2010); %a(2011); %a(2012); %a(2013); %a(2014); %a(2015); %a(2016);

	%drop(n_temp);
	
	n_phus1any_yr=.;
	n_phus1any_mo=.;
	n_phus1old_yr=.;
	n_phus1old_mo=.;
	do i = 1984 to 2016;
		do j = 1 to 12;
			if n_first(i,j)=1 then n_phus1any_yr=i;
			if n_first(i,j)=1 then n_phus1any_mo=j;

			if n_first(i,j)=1 and doec_tob=1 and i>=dobp_ccyy+61 then n_phus1old_yr=i;
			if n_first(i,j)=1 and doec_tob=1 and i>=dobp_ccyy+61 then n_phus1old_mo=j;
		end;
	end;

	%drop(n_first);
	drop n_first_s;

	* Flag observations where phus1any not the same as phus1old;
	if n_phus1old_yr^=n_phus1any_yr or n_phus1old_mo^=n_phus1any_mo then n_flag_phus=1;
		else n_flag_phus=0;

	* FIND DATE OF FIRST MBA/MBP;
	array efd_ccyy(1:150) efd_ccyy1-efd_ccyy150;
	array efd_mm(1:150)   efd_mm1-efd_mm150;
	
	n_mba1any_yr=input(efd_ccyy1, 4.);
	n_mba1any_mo=input(efd_mm1, 2.);
	n_mba1old_yr=.;
	n_mba1old_mo=.;
	do k=1 to 150;
		if efd_ccyy(k)>=dobp_ccyy+61 and doec_tob=1 and n_mba1old_yr=. then n_mba1old_yr=efd_ccyy(k);
		if efd_ccyy(k)>=dobp_ccyy+61 and doec_tob=1 and n_mba1old_mo=. then n_mba1old_mo=efd_mm(k);
	end;

	* Flag observations where mba1any not the same as mba1old;
	if n_mba1old_yr^=n_mba1any_yr or n_mba1old_mo^=n_mba1any_mo then n_flag_mba=1;
		else n_flag_mba=0;

	* Flag observations where PHUS and MBA are not the same;
	if n_mba1old_yr^=n_phus1old_yr then n_flag_phusmba=1;
		else n_flag_phusmba=0;

	* Drop variables ;
	drop i j k phusprim: efd_mm: efd_ccyy: mba: mbp:;

run;

%mend mbrphus;

* Seg 02 ;
filename inzip ZIP "&dir.mbr_seg02.zip"
                          member = "mbr_seg02.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg02.sas7bdat" ;
%unzip(work.mbr_seg02);

filename inzip ZIP "&dir.phus_seg02.zip"
                          member = "phus_seg02.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg02.sas7bdat" ;
%unzip(work.phus_seg02);

%mbrphus(02);

* Seg 03 ;
filename inzip ZIP "&dir.mbr_seg03.zip"
                          member = "mbr_seg03.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg03.sas7bdat" ;
%unzip(work.mbr_seg03);

filename inzip ZIP "&dir.phus_seg03.zip"
                          member = "phus_seg03.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg03.sas7bdat" ;
%unzip(work.phus_seg03);

%mbrphus(03);

* Seg 05 ;
filename inzip ZIP "&dir.mbr_seg05.zip"
                          member = "mbr_seg05.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg05.sas7bdat" ;
%unzip(work.mbr_seg05);

filename inzip ZIP "&dir.phus_seg05.zip"
                          member = "phus_seg05.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg05.sas7bdat" ;
%unzip(work.phus_seg05);

%mbrphus(05);

* Seg 06 ;
filename inzip ZIP "&dir.mbr_seg06.zip"
                          member = "mbr_seg06.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg06.sas7bdat" ;
%unzip(work.mbr_seg06);

filename inzip ZIP "&dir.phus_seg06.zip"
                          member = "phus_seg06.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg06.sas7bdat" ;
%unzip(work.phus_seg06);

%mbrphus(06);

* Seg 09 ;
filename inzip ZIP "&dir.mbr_seg09.zip"
                          member = "mbr_seg09.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg09.sas7bdat" ;
%unzip(work.mbr_seg09);

filename inzip ZIP "&dir.phus_seg09.zip"
                          member = "phus_seg09.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg09.sas7bdat" ;
%unzip(work.phus_seg09);

%mbrphus(09);

* Seg 10 ;
filename inzip ZIP "&dir.mbr_seg10.zip"
                          member = "mbr_seg10.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg10.sas7bdat" ;
%unzip(work.mbr_seg10);

filename inzip ZIP "&dir.phus_seg10.zip"
                          member = "phus_seg10.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg10.sas7bdat" ;
%unzip(work.phus_seg10);

%mbrphus(10);

* Seg 15 ;
filename inzip ZIP "&dir.mbr_seg15.zip"
                          member = "mbr_seg15.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg15.sas7bdat" ;
%unzip(work.mbr_seg15);

filename inzip ZIP "&dir.phus_seg15.zip"
                          member = "phus_seg15.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg15.sas7bdat" ;
%unzip(work.phus_seg15);

%mbrphus(15);

* Seg 16 ;
filename inzip ZIP "&dir.mbr_seg16.zip"
                          member = "mbr_seg16.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg16.sas7bdat" ;
%unzip(work.mbr_seg16);

filename inzip ZIP "&dir.phus_seg16.zip"
                          member = "phus_seg16.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg16.sas7bdat" ;
%unzip(work.phus_seg16);

%mbrphus(16);

* Seg 19 ;
filename inzip ZIP "&dir.mbr_seg19.zip"
                          member = "mbr_seg19.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg19.sas7bdat" ;
%unzip(work.mbr_seg19);

filename inzip ZIP "&dir.phus_seg19.zip"
                          member = "phus_seg19.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg19.sas7bdat" ;
%unzip(work.phus_seg19);

%mbrphus(19);

* Seg 20 ;
filename inzip ZIP "&dir.mbr_seg20.zip"
                          member = "mbr_seg20.sas7bdat";
filename ds "%sysfunc(getoption(work))\mbr_seg20.sas7bdat" ;
%unzip(work.mbr_seg20);

filename inzip ZIP "&dir.phus_seg20.zip"
                          member = "phus_seg20.sas7bdat";
filename ds "%sysfunc(getoption(work))\phus_seg20.sas7bdat" ;
%unzip(work.phus_seg20);

%mbrphus(20);

run;
quit;

